package com.medical.controller;
import com.medical.entity.AdminEntity;
import com.medical.service.AdminService;
import com.medical.tool.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @PostMapping("/login")
    public R login(String userAccount, String userPassword) {
        try {
            AdminEntity adminEntity= adminService.login(userAccount, userPassword);
            if (adminEntity == null) return new R(400, "账号或密码有误！");
            return new R(200, "登陆成功!", adminEntity);
        } catch (Exception exception) {
            exception.printStackTrace();
            return new R(500, "系统出现错误！");
        }
    }
    /**
     * 用户注册
     *
     * @param
     * @return
     */
    @PostMapping("/register")
    public R register(String userAccount, String userPassword) {
        boolean success = adminService.register(userAccount, userPassword);
        if (success) {
            return new R(200, "注册成功");
        } else {
            return new R(400, "用户名已存在，请重新注册");
        }
    }
    /**
     * 用户信息查询
     *
     * @return
     */
    @GetMapping("/selectList")
    public R selectList() {
        try {
            List<AdminEntity> adminEntity= adminService.selectList();
            if (adminEntity == null) return new R(400, "该用户不存在");
            return new R(200, "查询成功!", adminEntity);
        } catch (Exception exception) {
            exception.printStackTrace();
            return new R(500, "系统出现错误！");
        }
    }
    @PutMapping("/update")
    public R update(@RequestBody AdminEntity adminEntity) {
        int result = adminService.update(adminEntity);
        if (result > 0) {
            return new R(200, "更新成功", adminEntity);
        } else {
            return new R(400, "更新失败");
        }
    }
    /**
     * 用户注销
     *
     * @param
     * @return
     */
    @DeleteMapping("/logout")
    public R logout(@RequestParam String userAccount) {
        boolean result = adminService.delete(userAccount);
        if (result) {
            return new R(200, "注销成功");
        } else {
            return new R(400, "注销失败");
        }
        //return new R(200,"注销成功",userService.delete(userAccount));
    }
}
